package com.ruoyi.place.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.service.TcCommonService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.place.domain.PlaceElyd;
import com.ruoyi.place.domain.bo.PlaceElydBo;
import com.ruoyi.place.domain.vo.PlaceElydVo;
import com.ruoyi.place.mapper.PlaceElydMapper;
import com.ruoyi.place.service.IPlaceElydService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 易滥用毒品场所Service业务层处理
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@RequiredArgsConstructor
@Service
public class PlaceElydServiceImpl implements IPlaceElydService {

    private final PlaceElydMapper baseMapper;
    private final TcCommonService tcCommonService;

    /**
     * 查询易滥用毒品场所
     */
    @Override
    public PlaceElydVo queryById(Long elydId) {
        PlaceElydVo result = baseMapper.selectVoById(elydId);
        tcCommonService.renderTcInfo(Lists.newArrayList(result));
        return result;
    }

    /**
     * 查询易滥用毒品场所列表
     */
    @Override
    public TableDataInfo<PlaceElydVo> queryPageList(PlaceElydBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PlaceElyd> lqw = buildQueryWrapper(bo);
        Page<PlaceElydVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        tcCommonService.renderTcInfo(result.getRecords());
        return TableDataInfo.build(result);
    }

    /**
     * 查询易滥用毒品场所列表
     */
    @Override
    public List<PlaceElydVo> queryList(PlaceElydBo bo) {
        LambdaQueryWrapper<PlaceElyd> lqw = buildQueryWrapper(bo);
        List<PlaceElydVo> result = baseMapper.selectVoList(lqw);
        tcCommonService.renderTcInfo(result);
        return result;
    }

    private LambdaQueryWrapper<PlaceElyd> buildQueryWrapper(PlaceElydBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PlaceElyd> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), PlaceElyd::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getZhzt()), PlaceElyd::getZhzt, bo.getZhzt());
        lqw.eq(StringUtils.isNotBlank(bo.getDjjdshzt()), PlaceElyd::getDjjdshzt, bo.getDjjdshzt());
        return lqw;
    }

    /**
     * 新增易滥用毒品场所
     */
    @Override
    public Boolean insertByBo(PlaceElydBo bo) {
        PlaceElyd add = BeanUtil.toBean(bo, PlaceElyd.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setElydId(add.getElydId());
        }
        return flag;
    }

    /**
     * 修改易滥用毒品场所
     */
    @Override
    public Boolean updateByBo(PlaceElydBo bo) {
        PlaceElyd update = BeanUtil.toBean(bo, PlaceElyd.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PlaceElyd entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除易滥用毒品场所
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
